package id.mankel.commons.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

/**
 * @author dongxi.li
 */
public class Jacksons {

    public static String toJsonString(Object o) {
        if (null == o) {
            return null;
        }
        try {
            return getObjectWriter(o.getClass()).writeValueAsString(o);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }


    // ==================================================
    // private

    private static <T> ObjectWriter getObjectWriter(Class<T> tClass) {
        return new ObjectMapper().writerFor(tClass);
    }

}
